<?php
class Block_Model_Blocks extends Zen_List{
	public function __construct() {
		$this->table = new Zend_Db_Table ( 'block' );
	}
	
	public function getCache() {
		$cache = Zen_Cache_Factory::get('block');
		return $cache;
	}
	
	public function beforeCreate($params) {
		if ($params ['params'] != "") {
			$value = trim ( $params ['params'] );
			$value = preg_replace ( '/^\//', '', $value );
			$value = preg_replace ( '/\/$/', '', $value );
			$value = explode ( '/', $value );
			$data = array ();
			$i = 1;
			$keys = array ();
			$values = array ();
			foreach ( $value as $v ) {
				$i ++;
				if ($i % 2 == 0) {
					$keys [] = $v;
				} else {
					$values [] = $v;
				}
			}
			$params ['params'] = serialize ( array_combine ( $keys, $values ) );
		}
		
		if (isset ( $params ['date_start'] ) && ($params ['date_start'] == "")) {
			$params ['date_start'] = NULL;
		}
		if (isset ( $params ['date_end'] ) && ($params ['date_end'] == "")) {
			$params ['date_end'] = NULL;
		}
		return $params;
	}
	
	public function afterCreate($id, $params) {
		
		$block_rights = new Block_Model_Rights ();
		
		if (trim ( $params ['allow'] ) != '') {
			$allow = explode ( ',', $params ['allow'] );
			foreach ( $allow as $data ) {
				$data = trim ( $data );
				if ($data != '/') {
					$data = preg_replace ( "/^\\//", '', $data );
					$data = trim ( preg_replace ( "/\\/$/", '', $data ) );
				}
				
				if ($data != "") {
					$rights = array ('allow' => 1, 'url' => $data, 'block_id' => $id );
					$block_rights->create ( $rights );
				}
			}
		}
		
		if (trim ( $params ['deny'] ) != '') {
			$deny = explode ( ',', $params ['deny'] );
			foreach ( $deny as $data ) {
				$data = trim ( $data );
				if ($data != '/') {
					$data = preg_replace ( "/^\\//", '', $data );
					$data = trim ( preg_replace ( "/\\/$/", '', $data ) );
				}
				if ($data != "") {
					$rights = array ('allow' => 0, 'url' => $data, 'block_id' => $id );
					$block_rights->create ( $rights );
				}
			}
		}
	}
	public function afterUpdate($id, $params){
		$block_rights = new Block_Model_Rights ();
		$block_rights->getTable()->delete('block_id='.$id);
		if (trim ( $params ['allow'] ) != '') {
			$allow = explode ( ',', $params ['allow'] );
			foreach ( $allow as $data ) {
				$data = trim ( $data );
				if ($data != '/') {
					$data = preg_replace ( "/^\\//", '', $data );
					$data = trim ( preg_replace ( "/\\/$/", '', $data ) );
				}
				
				if ($data != "") {
					$rights = array ('allow' => 1, 'url' => $data, 'block_id' => $id );
					$block_rights->create ( $rights );
				}
			}
		}
		
		if (trim ( $params ['deny'] ) != '') {
			$deny = explode ( ',', $params ['deny'] );
			foreach ( $deny as $data ) {
				$data = trim ( $data );
				if ($data != '/') {
					$data = preg_replace ( "/^\\//", '', $data );
					$data = trim ( preg_replace ( "/\\/$/", '', $data ) );
				}
				if ($data != "") {
					$rights = array ('allow' => 0, 'url' => $data, 'block_id' => $id );
					$block_rights->create ( $rights );
				}
			}
		}
		
	}
	public function afterDelete($id){
		$block_rights = new Block_Model_Rights ();
		$block_rights->getTable()->delete('block_id='.$id);
	}
	public function beforeUpdate($id, $params) {
		if ($params ['params'] != "") {
			$value = trim ( $params ['params'] );
			$value = preg_replace ( '/^\//', '', $value );
			$value = preg_replace ( '/\/$/', '', $value );
			$value = explode ( '/', $value );
			
			$i = 1;
			$keys = array ();
			$values = array ();
			foreach ( $value as $v ) {
				$i ++;
				if ($i % 2 == 0) {
					$keys [] = $v;
				} else {
					$values [] = $v;
				}
			}
			
			$params ['params'] = serialize ( array_combine ( $keys, $values ) );
		}
		
		if (isset ( $params ['date_start'] ) && ($params ['date_start'] == "")) {
			$params ['date_start'] = NULL;
		}
		if (isset ( $params ['date_end'] ) && ($params ['date_end'] == "")) {
			$params ['date_end'] = NULL;
		}
		
		return $params;
	}
	
	public function afterRead($params) {
		if ($params ['params'] != "") {
			$p = '';
			$prm = unserialize ( $params ['params'] );
			foreach ( $prm as $key => $value ) {
				$p .= '/' . $key . '/' . $value;
			}
			$params ['params'] = preg_replace ( '/^\\//', '', $p );
		}
		
		$block_rights = new Block_Model_Rights ();
		$allow = $block_rights->select ()->getData ( "(`allow`=1) AND (`block_id`='" . $params ['id'] . "')" );
		
		if ($allow) {
			$d = array ();
			foreach ( $allow as $val ) {
				$d [] = $val ['url'];
			}
			$params ['allow'] = implode ( ',', $d );
		}
		
		$deny = $block_rights->select ()->getData ( "(`allow`=0) AND (`block_id`='" . $params ['id'] . "')" );
		if ($deny) {
			$d = array ();
			foreach ( $deny as $val ) {
				$d [] = $val ['url'];
			}
			$params ['deny'] = implode ( ',', $d );
		}
		
		return $params;
	}

}
?>